package com.example.test_activity.utils;

import com.example.test_activity.entity.Score;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/3/26/026.
 */

public class GetScoreEvery {

    private List<Map<String, String>> list = new ArrayList<>();

    //尚不及格成绩
    public List<Map<String, String>> getScoreNow(String html) {

            Document document = Jsoup.parse(html);

            Elements elements = document.select(".titleTop2");
            if (elements.isEmpty()) {
                return list;
            }
            Elements elements_1 = elements.get(0).select(".odd");

            for (int k = 0; k < elements_1.size(); ++k) {
                Elements elements_2 = elements_1.get(k).getAllElements();

                Score score = new Score();
                for (int i = 0; i < elements_2.size(); ++i) {
                    Element element = elements_2.get(i);
                    switch (i) {
                        case 3:
                            score.setName(element.text());
                            break;
                        case 5:
                            score.setCredit(element.text());
                            break;
                        case 6:
                            score.setProperties(element.text());
                            break;
                        case 7:
                            score.setScore(element.text());
                            break;
                        case 10:
                            score.setReason(element.text());
                            break;
                    }
                }
                getData(score);
            }

        return list;
    }

    //曾经不及格成绩
    public List<Map<String, String>> getScoreAgo(String html) {

            Document document = Jsoup.parse(html);

            Elements elements = document.select(".titleTop2");
            if (elements.isEmpty()) {
                return list;
            }
            Elements elements_1 = elements.get(1).select(".odd");

            for (int k = 0; k < elements_1.size(); ++k) {
                Elements elements_2 = elements_1.get(k).getAllElements();

                Score score = new Score();
                for (int i = 0; i < elements_2.size(); ++i) {
                    Element element = elements_2.get(i);
//                    if (element.attr("align").equals("center")) {
//                        Log.e("1", element.text() + " " + i);
//                    }
                    switch (i) {
                        case 3:
                            score.setName(element.text());
                            break;
                        case 5:
                            score.setCredit(element.text());
                        case 6:
                            score.setProperties(element.text());
                            break;
                        case 7:
                            score.setScore(element.text());
                            break;
                        case 10:
                            score.setReason(element.text());
                            break;
                    }
                }
                getData(score);
            }

        return list;
    }

    //全部及格成绩
    public List<Map<String,String>> getScoreOk(String html) {

            Document document = Jsoup.parse(html);
            Elements elements = document.select(".titleTop2");
            if (elements.isEmpty())
            {
                return list;
            }

            for (int j = 0; j < elements.size(); ++j) {
                Elements elements_1 = elements.get(j).select(".odd");

                for (int k = 0; k < elements_1.size(); ++k) {
                    Elements elements_2 = elements_1.get(k).getAllElements();

                    Score score = new Score();
                    for (int i = 0; i < elements_2.size(); ++i) {
                        Element element = elements_2.get(i);
                        switch (i) {
                            case 3:
                                score.setName(element.text());
                                break;
                            case 5:
                                score.setCredit(element.text());
                            case 6:
                                score.setProperties(element.text());
                                break;
                            case 7:
                                score.setScore(element.text());
                                break;
                            case 10:
                                score.setReason(element.text());
                                break;
                        }
                    }
                    getData(score);
                }
            }
        return list;
    }

    private void getData(Score score) {
        Map<String, String> map = new HashMap<>();
        map.put("name", score.getName());
        map.put("credit", score.getCredit());
        map.put("properties",score.getProperties());
        map.put("score", score.getScore());
        map.put("reason", score.getReason());

        list.add(map);
    }

}
